package com.zdd.ehe.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdd.ehe.entity.PriceData;
import com.zdd.ehe.entity.Role;
import com.zdd.ehe.result.Result;
import com.zdd.ehe.service.IPriceDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZhuiZhu
 * @since 2023-03-24
 */
@Api(tags="价格管理模块")
@RestController
@RequestMapping("/priceData")
public class PriceDataController {
    @Resource
    private IPriceDataService priceDataService;

    @ApiOperation("分页价格角色接口")
    @PostMapping("/getallprice")
    public Result getAllPrice(@RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize){
        Page<PriceData> pagex = new Page<>(page,pageSize);
        Page<PriceData> pageResult = priceDataService.getAllPrice(pagex);
        if(pageResult.getSize() == 0){
            return Result.error(4001,"无数据");
        }

        return Result.success("查询成功",pageResult);
    }



    @ApiOperation("根据商品名称goods查询")
    @GetMapping("/selectbygoods")
    public Result selectByGoods(@RequestParam("goods") String goods){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("goods",goods);
        List<PriceData> priceDataList = priceDataService.list(queryWrapper);
        if(priceDataList.size() !=0){
            Map<String,Object> data = new HashMap<>();
            data.put("priceDataList",priceDataList);
            data.put("total",priceDataList.size());
            data.put("size",10);
            data.put("current",1);
            data.put("pages",1);
            return Result.success("查询成功!",data);
        }else{
            return Result.error(4001,"未查询到该商品信息");
        }
    }



    @ApiOperation("新增价格数据接口")
    @PostMapping("/addpricedata")
    public Result addPriceData(@RequestBody PriceData priceData){
        int r = priceDataService.addPriceData(priceData);
        if(r == 1){
            return Result.success("添加成功");
        }else{
            return Result.error(4001,"添加失败");
        }
    }


    @ApiOperation("删除接口")
    @PostMapping("/deletepricedatabyid")
    public Result deletePriceDataById(@RequestParam("id") int id){
        PriceData priceData = priceDataService.getById(id);
        if(priceData == null){
            return Result.error(4001,"编号错误");
        }
        priceData.setStatus("0");
        int result = priceDataService.updatePriceDataById(priceData);
        if(result == -1){
            return Result.error(4002,"删除失败");
        }
        return Result.success("删除成功");
    }


    @ApiOperation("重启接口")
    @PostMapping("/restartpricedatabyid")
    public Result restartPriceDataById(@RequestParam int id){
        PriceData priceData = priceDataService.getById(id);
        if(priceData == null){
            return Result.error(4001,"编号错误");
        }
        priceData.setStatus("1");
        int result = priceDataService.updatePriceDataById(priceData);
        if(result == -1){
            return Result.error(4002,"重启失败");
        }
        return Result.success("重启成功");
    }


    @ApiOperation("修改价格数据接口")
    @PostMapping("/updatepricedata")
    public Result updatePriceData(@RequestBody PriceData priceData){
        int result = priceDataService.updatePriceDataById(priceData);
        if(result == -1){
            return Result.error("修改失败");
        }
        return Result.success("修改成功");
    }




}
